#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include "etcd.hpp"
#include "channel.hpp"
#include <thread>
#include <vector>
#include "utils.hpp"
#include "file.pb.h"
#include "base.pb.h"
#include "user.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式, fasle-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "这是服务中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "服务监控目录");

xchat::ServiceManager::ptr _user_channels;
// xchat::UserInfo user_info;
// std::string login_ssid;
// std::string new_nickname = "张三三三";

// TEST(用户子服务测试, 用户注册测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
//     user_info.set_nickname("张三");
//     xchat::UserRegisterReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_nickname(user_info.nickname());
//     req.set_password("123456");
 
//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::UserRegisterRsp> rsp(new xchat::UserRegisterRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.UserRegister(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());

// }

// TEST(用户子服务测试, 用户登录测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::UserLoginReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_nickname(user_info.nickname());
//     req.set_password("123456");
 
//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::UserLoginRsp> rsp(new xchat::UserLoginRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.UserLogin(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());

//     login_ssid = rsp->login_session_id();
// }

// TEST(用户子服务测试, 用户头像设置测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::SetUserAvatarReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     req.set_avatar(user_info.avatar());

//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::SetUserAvatarRsp> rsp(new xchat::SetUserAvatarRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.SetUserAvatar(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
 

//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());
// }

// TEST(用户子服务测试, 用户签名设置测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::SetUserDescriptionReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     req.set_description(user_info.description());
 
//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::SetUserDescriptionRsp> rsp(new xchat::SetUserDescriptionRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.SetUserDescription(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());
// }

// TEST(用户子服务测试, 用户昵称修改测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::SetUserNicknameReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     req.set_nickname(new_nickname);
 
//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::SetUserNicknameRsp> rsp(new xchat::SetUserNicknameRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.SetUserNickname(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());
// }

// TEST(用户子服务测试, 用户信息获取测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::GetUserInfoReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
 
//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::GetUserInfoRsp> rsp(new xchat::GetUserInfoRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.GetUserInfo(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());

//     ASSERT_EQ(user_info.user_id(), rsp->user_info().user_id());
//     ASSERT_EQ(rsp->user_info().nickname(), new_nickname);
//     ASSERT_EQ(rsp->user_info().description(), user_info.description());
//     ASSERT_EQ(rsp->user_info().avatar(), user_info.avatar());
//     ASSERT_EQ(rsp->user_info().phone(), "");

// }



// TEST(用户子服务测试, 批量用户信息获取测试)
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::GetMultiUserInfoReq req;
//     req.set_request_id(xchat::uuid());
//     req.add_users_id(user_info.user_id());
//     req.add_users_id("f7aa-b126b9c7-0001");

 
//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::GetMultiUserInfoRsp> rsp(new xchat::GetMultiUserInfoRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.GetMultiUserInfo(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());

//     auto users_map = rsp->mutable_users_info();
//     xchat::UserInfo user_info1 = (*users_map)[user_info.user_id()];
//     xchat::UserInfo user_info2 = (*users_map)["f7aa-b126b9c7-0001"];

//     ASSERT_EQ(user_info1.user_id(), user_info.user_id());
//     ASSERT_EQ(user_info1.nickname(), new_nickname);
//     ASSERT_EQ(user_info1.description(), user_info.description());
//     ASSERT_EQ(user_info1.avatar(), user_info.avatar());
//     ASSERT_EQ(user_info1.phone(), "");

//     ASSERT_EQ(user_info2.user_id(), "f7aa-b126b9c7-0001");
//     ASSERT_EQ(user_info2.nickname(), "张三");
//     ASSERT_EQ(user_info2.description(), "");
//     ASSERT_EQ(user_info2.avatar(), "");
//     ASSERT_EQ(user_info2.phone(), "");

// }
// std::string code_id;
// void get_code()
// {
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::PhoneVerifyCodeReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_phone_number(user_info.phone());

//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::PhoneVerifyCodeRsp> rsp(new xchat::PhoneVerifyCodeRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.GetPhoneVerifyCode(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());

//     code_id = rsp->verify_code_id();

// }

// TEST(用户子服务测试, 手机号注册测试)
// {
//     get_code();
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::PhoneRegisterReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_phone_number(user_info.phone());
//     req.set_verify_code_id(code_id);
//     ////////////////////////////////////////////////////////////////
//     ///应该有输入 TODO 
//     ////////////////////////////////////////////////////////////////
//     req.set_verify_code("1234");

//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::PhoneRegisterRsp> rsp(new xchat::PhoneRegisterRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.PhoneRegister(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());

// }

// TEST(用户子服务测试, 手机号登录测试)
// {
//     get_code();
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::PhoneLoginReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_phone_number(user_info.phone());
//     req.set_verify_code_id(code_id);
//     ////////////////////////////////////////////////////////////////
//     ///应该有输入 TODO 
//     ////////////////////////////////////////////////////////////////
//     req.set_verify_code("1234");

//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::PhoneLoginRsp> rsp(new xchat::PhoneLoginRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.PhoneLogin(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());
//     std::cout << "手机登录会话id" << rsp->login_session_id() << std::endl;
// }

// TEST(用户子服务测试, 手机号设置测试)
// {
//     get_code();
//     auto channel = _user_channels->choose(FLAGS_user_service);
//     ASSERT_TRUE(channel != nullptr);
   
//     xchat::SetUserPhoneNumberReq req;
//     req.set_request_id(xchat::uuid());
//     req.set_user_id(user_info.user_id());

//     std::string new_phone = "13254678901";
//     req.set_phone_number(new_phone);
//     req.set_phone_verify_code_id(code_id);
//     ////////////////////////////////////////////////////////////////
//     ///应该有输入 TODO 
//     ////////////////////////////////////////////////////////////////
//     req.set_phone_verify_code("1234");

//     xchat::UserService_Stub stub(channel.get());
//     std::unique_ptr<xchat::SetUserPhoneNumberRsp> rsp(new xchat::SetUserPhoneNumberRsp());
//     std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
//     stub.SetUserPhoneNumber(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
//     ASSERT_FALSE(cntl->Failed());
//     ASSERT_TRUE(rsp->success());
    
// }

void reg_user(const std::string &nickname, const std::string& password)
{
    xchat::UserInfo user_info;
    auto channel = _user_channels->choose(FLAGS_user_service);
    ASSERT_TRUE(channel != nullptr);
    xchat::UserRegisterReq req;
    req.set_request_id(xchat::uuid());
    req.set_nickname(nickname);
    req.set_password(password);
 
    xchat::UserService_Stub stub(channel.get());
    std::unique_ptr<xchat::UserRegisterRsp> rsp(new xchat::UserRegisterRsp());
    std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
    stub.UserRegister(cntl.get(), &req, rsp.get(), nullptr); // 同步调用


}
void set_user_avatar(const std::string &uid, const std::string& avatar)
{
    auto channel = _user_channels->choose(FLAGS_user_service);
    ASSERT_TRUE(channel != nullptr);
   
    xchat::SetUserAvatarReq req;
    req.set_request_id(xchat::uuid());
    req.set_user_id(uid);
    req.set_avatar(avatar);

    xchat::UserService_Stub stub(channel.get());
    std::unique_ptr<xchat::SetUserAvatarRsp> rsp(new xchat::SetUserAvatarRsp());
    std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
    stub.SetUserAvatar(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
 
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    
    xchat::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1.先构造Rpc信道管理对象
    _user_channels = std::make_shared<xchat::ServiceManager>();
    _user_channels->declared(FLAGS_user_service);
    auto put_cb = std::bind(&xchat::ServiceManager::onServiceOnline, _user_channels.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&xchat::ServiceManager::onServiceOffline, _user_channels.get(), std::placeholders::_1, std::placeholders::_2);
    //2.构造服务发现对象
    xchat::Discovery::ptr dclient = std::make_shared<xchat::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    
    // user_info.set_description("this is a test user");
    // user_info.set_nickname("张三");
    // user_info.set_user_id("21df-12664a1c-0000");
    // user_info.set_description("张三个性签名");
    // user_info.set_phone("18345678910");
    // user_info.set_avatar("张三头像数据");
    // set_user_avatar("21df-12664a1c-0000", "张三三三头像数据");
    // set_user_avatar("f7aa-b126b9c7-0001", "张三头像数据");

    // reg_user("李四", "123424156");
    // reg_user("王五", "5235342");
    //reg_user("赵六", "5235dwadaw342");
    reg_user("王五五", "14456dwa2dwa");

    //set_user_avatar("21df-12664a1c-0000", "张三三三头像数据");
    //set_user_avatar("f7aa-b126b9c7-0001", "张三头像数据");
    // set_user_avatar("41d1-b0335c8f-0000", "李四头像数据");
    // set_user_avatar("1460-080c2225-0001", "王五头像数据");

    return RUN_ALL_TESTS();
}